package specifications;

import java.lang.reflect.Field;

import entities.Client;
import entities.Flight;
import entities.Order;

public class OrderSpecification implements Specification<Order>{
	private String attribute;
    private Object value;

    public OrderSpecification(String attribute, Object value) {
        this.attribute = attribute;
        this.value = value;
    }

    @Override
    public final boolean isSatisfiedBy(Order candidate) {
        Field field = null;
		try {
			field = candidate.getClass().getDeclaredField(this.getAttribute());
		} catch (NoSuchFieldException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Object candidateValue = null;
		try {
			field.setAccessible(true);
			candidateValue = field.get(candidate);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return isSatisfiedByValue(candidateValue);
    }

    protected boolean isSatisfiedByValue(Object candidateValue) {
    	if(this.getAttribute() == "orderFlight") {
    		Flight c = (Flight) candidateValue;
    		return c.getFlightNo().equalsIgnoreCase((String)getValue());
    	}
    	else {
    		if(this.getAttribute() == "orderClient") {
        		Client c = (Client) candidateValue;
        		return c.getPassport().equalsIgnoreCase((String)getValue());
        	}
    		else
    		{
    			return candidateValue.equals(getValue());
    		}
    	}
    }

    public String getAttribute() {
        return attribute;
    }

    public Object getValue() {
        return value;
    }
}
